//
//  Controller07.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/3/3.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit

class Controller07: UIViewController,UITableViewDelegate,UITableViewDataSource{

    let myOldData = ["a","b","👄","🌺"];
    let myNewData = ["a","b","👄","🌺",
                     "cccc\nccc","dadadadada\ndadaddahjdhadhsakjdhkashgdsagdhaghdajdfgajfdgajfdahdghajhdjahdagdhadagdhagdhaghdajdaghdagka","🌷🌷🌷🌷🌷🌷🌷🌷🌷🌷","🌳🌸☘🌸☘🌸☘🌸☘🌸☘🌸☘🌸☘🌸☘🌸☘🌸☘🌸☘🌸☘☘🌸☘🌸☘🌸☘🌸🌳end"];
    var datas : [String] = [];
    var tableview : UITableView!;
    let myIndentify = "ABC"
    let refreshControl = UIRefreshControl();

    
    override func viewDidLoad() {
        super.viewDidLoad()

        datas = myOldData;
        
        
      //refreshControl
        refreshControl.tintColor = .white;
        refreshControl.attributedTitle = NSAttributedString(string: currentDateString(),attributes:[NSForegroundColorAttributeName:UIColor.white]);
        refreshControl.addTarget(self, action: #selector(refreshAction(sender:)),for:.valueChanged);
        
     //tableview
        tableview = UITableView(frame: UIScreen.main.bounds, style: .plain);
        tableview.dataSource = self;
        tableview.delegate = self;
        tableview.backgroundColor = .clear;
        tableview.tableFooterView = UIView();
        tableview.rowHeight = UITableViewAutomaticDimension;
        view.addSubview(tableview);
        tableview.refreshControl = refreshControl;
        
    }
    
    func refreshAction(sender : UIRefreshControl){
        DispatchQueue.global().async {
            Thread.sleep(forTimeInterval: 3);
            DispatchQueue.main.async(execute: {
                self.refreshControl.attributedTitle = NSAttributedString(string: self.currentDateString(),attributes:[NSForegroundColorAttributeName:UIColor.white]);
                self.datas = self.myNewData;
                self.tableview.reloadData();
                
                sender.endRefreshing();
            })
        }
    }
    
    func currentDateString() -> String {
        let date = Date();
        let format = DateFormatter();
        format.dateFormat = "yyyy-MM-dd HH:mm:ss";
        let string = format.string(from: date);
        return string;
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableview.dequeueReusableCell(withIdentifier: myIndentify);
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: myIndentify);
            cell!.textLabel?.textColor = .white;
            cell!.textLabel?.font = UIFont.systemFont(ofSize: 17);
            cell!.textLabel?.numberOfLines = 0;
            cell!.backgroundColor = .clear;
        }
        cell?.textLabel?.text = datas[indexPath.row];
        return cell!;
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    


}
